﻿/*
 *  Copyright 2012 HEINER DIRECT GmbH & Co. KG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 * @Author Rafael Dabrowski
 * 
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; 
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using NCalc; 

namespace HDI.SharePoint.Webparts.HDIAggregatedView.HDIAggregatedViewWP
{
    class Helper
    {
        public static string Serialize<T>(T obj)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
            MemoryStream ms = new MemoryStream();
            serializer.WriteObject(ms, obj);
            string retVal = Encoding.UTF8.GetString(ms.ToArray());
            return retVal;
        }

        public static T Deserialize<T>(string json)
        {
            try
            {
                T obj = Activator.CreateInstance<T>();
                MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
                obj = (T)serializer.ReadObject(ms);
                ms.Close();
                return obj;
            }
            catch
            {
                return default(T);
            }
        }

        public static double calculate(string x)
        {
            try
            {
                Expression e = new Expression(x);
                e.Parameters["E"] = Math.E;
                e.Parameters["Pi"] = Math.PI;

                e.EvaluateFunction += delegate(string name, FunctionArgs args)
                {
                    if (name == "Fac")
                    {
                        int number = int.Parse(args.Parameters[0].Evaluate().ToString());
                        if (number <= 20 && number >= 0)
                            args.Result = factors64[number];
                        else
                            throw new ArgumentOutOfRangeException("Die Fakultät kann nur bis 20 berechnet werden. Momentaner Wert: " + number.ToString());
                    }
                };
                return double.Parse(e.Evaluate().ToString());
            }
            catch (Exception ex)
            {
                throw new Exception("Es gibt ein Fehler in der Formel: " + x + "; Genauer Fehler: " + ex.ToString());
            }
        }

        static Int64[] factors64 = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320,
362880, 3628800, 39916800, 479001600, 6227020800, 87178291200, 
1307674368000, 20922789888000, 355687428096000, 6402373705728000, 
121645100408832000, 2432902008176640000 };
    }


    
}
